import os
import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

# 导入路由器
from py_files.pdf2md import router as pdf2md_router
from py_files.image2md import router as image2md_router
from py_files.image2pdfself import router as image2pdf_router
from py_files.pdf2imageself import router as pdf2image_router
from py_files.pdf2md import ensure_directory_exists

# 默认目录
DEFAULT_INPUT_DIR = "input_files"
DEFAULT_OUTPUT_DIR = "output_files"

# 创建FastAPI应用
app = FastAPI(
    title="文件转换API",
    description="提供PDF转Markdown、图片转Markdown、图片转PDF、PDF转图片等功能的API服务",
    version="1.0.0"
)

# 在启动时确保默认目录存在
@app.on_event("startup")
async def startup_event():
    ensure_directory_exists(DEFAULT_INPUT_DIR)
    ensure_directory_exists(DEFAULT_OUTPUT_DIR)
    # 挂载静态文件目录，用于下载生成的文件
    app.mount("/outputs", StaticFiles(directory=DEFAULT_OUTPUT_DIR), name="outputs")

# 包含路由器
app.include_router(pdf2md_router)
app.include_router(image2md_router)
app.include_router(image2pdf_router)
app.include_router(pdf2image_router)

@app.get("/")
async def root():
    """API根路径，提供简单的信息"""
    return {
        "message": "文件转换API服务",
        "endpoints": {
            "PDF转Markdown": {
                "文本PDF转Markdown": "/pdf2md/text",
                "图像PDF转Markdown": "/pdf2md/image"
            },
            "图片转Markdown": "/image2md/convert",
            "图片转PDF": "/image2pdf/convert",
            "PDF转图片": "/pdf2image/convert"
        }
    }

if __name__ == "__main__":
    # 启动FastAPI服务
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)